<?php

/**
 * @author Lim Yi Le
 */
class libraryupdates extends CI_Controller{

/*
|--------------------------------------------------------------------------
| index (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function checks the category and content size (summary or full) of library updates
| and returns the corresponding parsed library updates results as JSON and call the 
| function to wrap the response in the requested callback. 
|
*/   
    function index($category, $contentSize) {
        
        $this->load->model('LibraryUpdates_Model');        
        $libraryUpdatesGeneralUrl = $this->config->item('libraryUpdatesGeneralUrl');
        $libraryUpdates_10results_Url = $this->config->item('libraryUpdates_10results_Url');
        
        // URL specially for All Category
        $libraryUpdatesGeneralUrl_Default = $this->config->item('libraryUpdatesGeneralUrl_Default');
        
        // captures Url Encoding problem with the '+' sign
        if ($category == 'Cher%20Says'){
            $category = 'Cher+Says...';
        }
        
        $feed_url = $libraryUpdatesGeneralUrl . $category . $libraryUpdates_10results_Url;
        
        if ($category == 'All'){
            $feed_url = $libraryUpdatesGeneralUrl_Default;
        }
        
        $simplexml_feed = simplexml_load_file($feed_url);
        $output = $this->LibraryUpdates_Model->parseLibraryUpdatesXML($simplexml_feed, $category, $contentSize);
        $this->retJsonP($output); 
    }
    
 /*
|--------------------------------------------------------------------------
| retJsonP (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function wraps the response in the requested callback. 
|
*/    
    function retJsonP($output) {
        $callback = $_REQUEST['callback'];

        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($output) . ');';
        } else {
            header('Content-Type: application/x-json');
            echo json_encode($output);
        }
    }
}

?>
